package com.taikang.dao;

import com.taikang.dao.base.DefaultCommonDaoImpl;
import com.taikang.entity.SpecialServiceOrder;
import com.taikang.util.page.Page;
import com.taikang.util.page.Pagination;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhai on 16/5/10.
 */
@Repository
public class GrabDao extends DefaultCommonDaoImpl<SpecialServiceOrder> {

    private SqlCallback listCallback;

    public Pagination getGrabListByAgent(Integer agentId, Page page) {
        Sql sql = Sqls.create(getDao().sqls().get("getGrabListByAgent"));
        sql.setParam("agentId", agentId);

        if (null == listCallback) {
            listCallback = createSqlCallBack(true, getEntityClass());
        }
        sql.setCallback(listCallback);
        return listPage(page.getPage(), page.getLimit(), sql);
    }

    public Object getServiceInfoByOrderId(Integer orderId) {
        Map<String, Object> serviceInfo = new HashMap<>();

        if(null != getDao().fetch("clearing_record", Cnd.where("OrderID", "=", orderId))) {
            Record r1 = getDao().fetch("clearing_record", Cnd.where("OrderID", "=", orderId));
            Record r2 = getDao().fetch("clearing_service", Cnd.where("ClearingID", "=", r1.get("ClearingID")));
            Record r3 = getDao().fetch("clearing_type", Cnd.where("ClearingTypeID", "=", r2.get("ClearingTypeID")));
            serviceInfo.put("price", r2.get("price"));
            serviceInfo.put("serviceName", r3.get("service"));
            serviceInfo.put("serviceType", "保洁");
            return serviceInfo;
        } else if(null != getDao().fetch("repar_record", Cnd.where("OrderID", "=", orderId))) {
            Record r1 = getDao().fetch("repar_record", Cnd.where("OrderID", "=", orderId));
            serviceInfo.put("price", r1.get("price"));
            serviceInfo.put("serviceName", r1.get("equipment" + "-" + r1.get("fault")));
            serviceInfo.put("serviceType", "维修");
            return serviceInfo;
        } else if(null != getDao().fetch("phusical_record", Cnd.where("OrderID", "=", orderId))) {
            Record r1 = getDao().fetch("phusical_record", Cnd.where("OrderID", "=", orderId));
            Record r2 = getDao().fetch("phusical_service", Cnd.where("PhysicalID", "=", r1.get("PhysicalID")));
            Record r3 = getDao().fetch("physical_type", Cnd.where("PhysicalTypeID", "=", r2.get("PhysicalTypeID")));
            serviceInfo.put("price", r1.get("price"));
            serviceInfo.put("serviceName", r3.get("service"));
            serviceInfo.put("serviceType", "理疗");
            return serviceInfo;
        } else if(null != getDao().fetch("cook_record", Cnd.where("OrderID", "=", orderId))) {
            Record r1 = getDao().fetch("cook_record", Cnd.where("OrderID", "=", orderId));
            Record r2 = getDao().fetch("cook_service", Cnd.where("CookID", "=", r1.get("CookID")));
            serviceInfo.put("price", r1.get("price"));
            serviceInfo.put("serviceName", r2.get("dishes" + "-" + r2.get("service")));
            serviceInfo.put("serviceType", "私厨");
            return serviceInfo;
        } else if(null != getDao().fetch("shuttle_record", Cnd.where("OrderID", "=", orderId))) {
            Record r1 = getDao().fetch("shuttle_record", Cnd.where("OrderID", "=", orderId));
            Record r2 = getDao().fetch("shuttle_service", Cnd.where("ShuttleID", "=", r1.get("ShuttleID")));
            serviceInfo.put("price", r1.get("price"));
            serviceInfo.put("serviceName", r2.get("start") + "-" + r2.get("end"));
            serviceInfo.put("serviceType", "接送");
            return serviceInfo;
        } else {
            SpecialServiceOrder specialServiceOrder = fetch(orderId);
            delete(specialServiceOrder);
            return null;
        }
    }
}
